package com.saver.modules.sys.controller;


import com.saver.common.db.JdbcTool;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.R;
import com.saver.modules.sys.entity.SysDownloadEntity;
import com.saver.modules.sys.service.SysDownloadService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 相关下载
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午11:22
 */
@RestController
@RequestMapping("/sys/download")
public class SysDownloadController extends AbstractController {

    @Autowired
    private SysDownloadService sysDownloadService;

    @Autowired
    private JdbcTool jdbcTool;

    /**
     * 分页
     *
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = sysDownloadService.queryPage(params);
        return R.ok().put("page", pageUtils);
    }

    @GetMapping("/all")
    public R all() {
        List<Map<String, Object>> list = jdbcTool.list("select uuid,title from sys_download");
        return R.ok().put("data",list);
    }

    /**
     * 文件信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String fileUuid) {
        SysDownloadEntity sysDownloadEntity = sysDownloadService.get(fileUuid);
        return R.ok().put("file", sysDownloadEntity);
    }

    /**
     * 保存文件信息
     */
    @PostMapping("/save")
    public R save(@RequestBody SysDownloadEntity entity) {
        sysDownloadService.save(entity, getUserId());
        return R.ok();
    }

    /**
     * 修改文件信息
     */
    @PostMapping("/update")
    public R update(@RequestBody SysDownloadEntity entity) {
        sysDownloadService.update(entity);
        return R.ok();
    }


    /**
     * 删除文件并附件
     *
     * @param uuid
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody String uuid) {
        return sysDownloadService.delete(uuid);
    }

}
